﻿using ConfExpert.Forms.Base;

using DevTeamIT;
using Extensions;
using ITaskManager.InfomobilWS;
using System;
using System.Collections.Generic;
using System.Data;
using System.Reflection;
using System.ServiceModel;
using System.Text;

namespace ITaskManager.Forms.IT
{
    public partial class Infomobil : XForm
    {
        public Infomobil()
        {
            InitializeComponent();
        }

        private void Infomobil_Load(object sender, EventArgs e)
        {
            InfomobilWS.InfomobilClient infomobil = new InfomobilWS.InfomobilClient();
            string userName = Session.InfomobilUsername;
            string password = Session.InfomobilPassword;

            Encoding iso = Encoding.GetEncoding("ISO-8859-1");

            BasicHttpBinding binding = (BasicHttpBinding)infomobil.Endpoint.Binding;

            //binding.TextEncoding = iso;
            binding.MaxBufferSize = Int32.MaxValue;
            binding.MaxReceivedMessageSize = Int32.MaxValue;
            string token = infomobil.register(userName, password, "Karbel", "tr");

            if (token != string.Empty)
            {
                dataMobileContainer[] cn = infomobil.getMobiles(token);
                List<dataMobile> m = ShowOnGrid<dataMobile, dataMobileContainer>(cn);
                
                
                dataPosition[] dp1 = infomobil.getGpsLastPositions(token, m.ToArray());
                ShowOnGrid<dataPosition>(dp1);
                /*
                long mobile = 0;
                dataMobile r = m.Find(x => x.alias.Contains("KARAKAYA"));
                //DataRow[] r = m.Select("alias like '%Sargun%'");
                if (r != null)
                    mobile = r.mobile;

                string basla = DateTime.Now.AddDays(-70).ToString();
                string bitir = DateTime.Now.ToString();
                //dataPosition[] dp2 = getMobilePositionsBetweenInterval
                //dataTrip[] dp2 = infomobil.getMobileTripsBetweenInterval(token, r, "2015-12-1 10:00", "2015-12-31 10:00");
                //ShowOnGrid<dataTrip>(dp2);

                //double totalkm = infomobil.getSumDistanceWithInRange(token, m.ToArray(), "2015-12-1 10:00", "2015-12-31 10:00");
                //XtraMessageBox.Show(totalkm.ToStr());
                
                dataPosition[] dp2 = infomobil.getGpsLastPositions(token, m.ToArray());
                ShowOnGrid<dataPosition>(dp2);

                //dataTelemetry dt = infomobil.getLastTelemetry(token, mobile, new dataNameValue[] {"1", "1"});
                 */
            }
        }

        private List<T> ShowOnGrid<T, T2>(T2[] cn)
        {
            List<T> m = new List<T>();
            if (cn != null)
            {
                DataTable dt = new DataTable();
                PropertyInfo[] properties = typeof(T).GetProperties();
                foreach (PropertyInfo property in properties)
                    dt.Columns.Add(property.Name, property.PropertyType);

                foreach (T2 dmc in cn)
                {
                    Type myType = typeof(T2);
                    PropertyInfo myPropInfo = myType.GetProperty(typeof(T).Name);
                    T dm = (T)myPropInfo.GetValue(dmc, null);

                    DataRow row = dt.NewRow();
                    foreach (PropertyInfo property in properties)
                        row[property.Name] = property.GetValue(dm, null);
                    dt.Rows.Add(row);

                    m.Add(dm);
                }

                gridView1.Init(dt);
                gridView1.AddSummary();
                gridView1.BestFitColumns();
            }
            return m;
        }


        private List<T> ShowOnGrid<T>(T[] cn)
        {
            DataTable dt = new DataTable();
            PropertyInfo[] properties = typeof(T).GetProperties();
            foreach (PropertyInfo property in properties)
                dt.Columns.Add(property.Name, property.PropertyType);

            List<T> m = new List<T>();

            foreach (T dm in cn)
            {
                DataRow row = dt.NewRow();
                foreach (PropertyInfo property in properties)
                    row[property.Name] = property.GetValue(dm, null);
                dt.Rows.Add(row);

                m.Add(dm);
            }

            gridView1.Init(dt);
            gridView1.AddSummary();
            gridView1.BestFitColumns();
            return m;
        }
    }
}
